package swimv2gm.client.user;

import java.io.IOException;

import javax.naming.*;
import javax.servlet.http.*;

import swimv2gm.bl.session.*;

@SuppressWarnings("serial")
public class Signup extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
		doPost(request,response);
	}

	@Override
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {
		UserManager man;
		try {
			man = (UserManager)InitialContext.doLookup("UserManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}
		long uid = -1;
		try {
			uid = man.createUser(request.getParameter("email"),request.getParameter("nome"),
					request.getParameter("cognome"),request.getParameter("password"));
		} catch (ExistingDataException e) {
			response.sendRedirect("/SWIMv2gm/user/signup.jsp?errortype=existing");
			return;
		}
		if (uid < 0) {
			response.sendRedirect("/SWIMv2gm/user/signup.jsp?errortype=invalid");
		}
		else {
			request.getSession().setAttribute("admin",false);
			request.getSession().setAttribute("user",uid);
			response.sendRedirect("/SWIMv2gm/user/Profile?executed=newuser&uid=" + uid);
		}
	}

}
